package com.wc.alorithm_luogu.P1786;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @author congege
 * @create 2023:04:27 21:04
 * @describe https://www.luogu.com.cn/problem/P1786
 * 帮贡排序
 */
public class Main {
    public static void main(String[] args) {
        People Bangzhu = null;
        People[] FuBangzhu = new People[2];
        int FuBangzhuIndex = 0;
        People[] HuFa = new People[2];
        int HuFaIndex = 0;
        People[] ZhangLao = new People[4];
        int ZhangLaoIndex = 0;
        People[] TangZhu = new People[7];
        int TangZhuIndex = 0;
        People[] JingYing = new People[25];
        int JingYingIndex = 0;
        People[] BangZhong = new People[110 - 25 - 7 - 4 - 2 - 2 - 1];
        int BangZhongIndex = 0;
        ArrayList<People> careers = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.next());
        for (int i = 0; i < n; i++) {
            People people = new People();
            people.name = sc.next();
            people.career = sc.next();
            people.contribution = Integer.parseInt(sc.next());
            people.grade = Integer.parseInt(sc.next());
            people.index = i;
            if (people.career.equals("BangZhu")) {
                Bangzhu = people;
            } else if (people.career.equals("FuBangZhu")) {
                FuBangzhu[FuBangzhuIndex] = people;
                FuBangzhuIndex++;
            } else {
                careers.add(people);
            }
        }
        careers.sort(new Comparator<People>() {
            // 从大到小排序
            @Override
            public int compare(People people1, People people2) {
                return people2.contribution - people1.contribution;
            }
        });

        // 两位护法
        int index = 0;
        int size = careers.size();
        String[] ret = assign(careers, HuFa, 0, 2, "HuFa").split(" ");
        index = Integer.parseInt(ret[0]);
        HuFaIndex = Integer.parseInt(ret[1]);

        // 四位长老
        ret = assign(careers, ZhangLao, index, 4, "ZhangLao").split(" ");
        index = Integer.parseInt(ret[0]);
        ZhangLaoIndex = Integer.parseInt(ret[1]);

        // 七位堂主
        ret = assign(careers, TangZhu, index, 7, "TangZhu").split(" ");
        index = Integer.parseInt(ret[0]);
        TangZhuIndex = Integer.parseInt(ret[1]);

        // 二十五个精英
        ret = assign(careers, JingYing, index, 25, "JingYing").split(" ");
        index = Integer.parseInt(ret[0]);
        JingYingIndex = Integer.parseInt(ret[1]);

        while (index < size) {
            careers.get(index).career = "BangZhong";
            BangZhong[BangZhongIndex++] = careers.get(index);
            index++;
        }
        Comparator<People> gradeComparator = new Comparator<People>() {
            @Override
            public int compare(People people1, People people2) {
                if (people1.grade == people2.grade) {
                    return people1.index - people2.index;
                }
                return people2.grade - people1.grade;
            }
        };
        sortPeopleByGrade(FuBangzhu, FuBangzhuIndex, (Comparator<People>) gradeComparator);
        sortPeopleByGrade(HuFa, HuFaIndex, gradeComparator);
        sortPeopleByGrade(ZhangLao, ZhangLaoIndex, gradeComparator);
        sortPeopleByGrade(TangZhu, TangZhuIndex, gradeComparator);
        sortPeopleByGrade(JingYing, JingYingIndex, gradeComparator);
        sortPeopleByGrade(BangZhong, BangZhongIndex, gradeComparator);

        System.out.println(Bangzhu);
        print(FuBangzhu, FuBangzhuIndex);
        print(HuFa, HuFaIndex);
        print(ZhangLao, ZhangLaoIndex);
        print(TangZhu, TangZhuIndex);
        print(JingYing, JingYingIndex);
        print(BangZhong, BangZhongIndex);
    }

    // 排序
    private static void sortPeopleByGrade(People[] peoples, int peoplesIndex, Comparator<People> gradeComparator) {
        Arrays.sort(peoples, 0, peoplesIndex, gradeComparator);
    }

    // 打印
    private static void print(People[] peoples, int peopleIndex) {
        for (int i = 0; i < peopleIndex; i++) {
            System.out.println(peoples[i]);
        }
    }

    // 赋值位
    private static String assign(ArrayList<People> careers, People[] peoples, int curCareersIndex, int maxIndex, String name) {
        int size = careers.size();
        int careerIndex = 0;
        for (int i = 0; i < maxIndex; i++) {
            if (curCareersIndex < size) {
                careers.get(curCareersIndex).career = name;
                peoples[i] = careers.get(curCareersIndex);
                careerIndex++;
            } else {
                break;
            }
            curCareersIndex++;
        }
        return curCareersIndex + " " + careerIndex;
    }

    public static class People {
        // 名字
        public String name;
        // 职位
        public String career;
        // 贡献
        public int contribution;
        // 等级
        public int grade;
        // 位置
        public int index;

        @Override
        public String toString() {
            return name + " " + career + " " + grade;
        }
    }
}
